<?php

namespace app\service;

//二级市场服务层


use app\model\UserCard;
use app\model\UserMarket;

class UserMarketService extends BaseService
{

    public function list($user_id, $type, $page, $limit): array
    {
        if ($type == SECOND_MARKET_TYPE['所有']) {
            $user_market = (new UserMarket())->with(['user', 'user_card'])->paginate($limit);
        } else {
            $user_market = (new UserMarket())->with(['user', 'user_card'])->where('type', $type)->paginate($limit);
        }
        return $this->returnSuccess($user_market);
    }

    //添加寄售
    public function add_sale($user_id, $user_card_id, $price, $end_time): array
    {

        //查询用户是否有卡
        $user_card = (new UserCard())->find($user_card_id);
        if (!$user_card) {
            return $this->returnFail('用户卡不存在');
        }

        $user_market = new UserMarket();
        $user_market->user_id = $user_id;
        $user_market->user_card_id = $user_card_id;
        $user_market->price = $price;
        $user_market->end_time = date('Y-m-d H:i:s', time() + $end_time);
        $user_market->save();
        return $this->returnSuccess($user_market);
    }


}